﻿using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Wimm.Common.Entities;
using Wimm.Common.Entities.Utils;
using Wimm.Common.Interfaces.Repositories;
using Wimm.Common.Interfaces.Services;
using Windows.UI.Xaml;
using Wimm.CustomContols;
using Wimm.UI.Login;
using Wimm.Common;

namespace Wimm.UI.ViewModels
{
    public class MultiLoginViewModel:ViewModelBase
    {
        private String _userName;
        private String _password;
        private String _errorMessage;
        private bool _visibileBool = false;

        private INavigationService _navigationService;
        private readonly RelayCommand _loginCommand;
        private readonly RelayCommand _registrationCommand;

        private readonly ILoginService _loginService;
        private IUserService _userService;
        private ObservableCollection<User> _users;
        private User _selectedUser;
       
        public MultiLoginViewModel(INavigationService navigationService,IUserService userService, ILoginService loginService)
        {
            _loginService = loginService;
            _userService = userService;
            _navigationService = navigationService;
            
            _registrationCommand = new RelayCommand(DoRegistration, CanRegistration);
            _loginCommand = new RelayCommand(DoLogin, CanLogin);

            this.Language = Windows.Globalization.ApplicationLanguages.Languages.First() ?? "en-US";
        }

        public async Task InitModelDataAsync()
        {
            var users = await _userService.LoadUsers();
            _users = new ObservableCollection<User>(users);
            
            if (_users.Count == 1)
            {
                this._visibileBool = false;
            }
            if (_users.Count > 1)
            {
                this._visibileBool = true;
                SelectedUser = _users[0];
            }
        }

        public ICommand RegistrationCommand
        {
            get { return _registrationCommand; }
        }

        public ICommand LoginCommand
        {
            get { return _loginCommand; }
        }

        private bool CanRegistration()
        {
            return true;
        }

        private void DoRegistration()
        {
            this.ErrorMessage = "";
            _navigationService.NavigateTo("registration", typeof(Registration));
        }

        private bool CanLogin()
        {
            return !String.IsNullOrWhiteSpace(_password) && _password.Length > 3;
        }

        private bool VisibileBool
        {
            set
            {
                _visibileBool = value;
               
            }
        }
        public Visibility ComboboxVisibile
        {
            get { return _visibileBool ? Visibility.Visible : Visibility.Collapsed; }
        }
        private async void DoLogin()
        {

            String res = await _loginService.GetAuthenticationResult();

            if (res == "multi")
            {
                if (_users == null) return;
                if (_users.Count == 1)
                {
                   var u = await _loginService.UserPassword(_password);

                   if (u != null)
                   {
                      this.Password = null;

                      this.ErrorMessage = "LoginWelcome".GetStringFromResource(this.Language);

                      await Task.Delay(500);

                      this.ErrorMessage = null;

                      _navigationService.NavigateTo("hub", u);
                   }
                   else
                   {
                      this.Password = null;
                      this.ErrorMessage = "LoginError".GetStringFromResource(this.Language);
                   }
                }


                if(_users.Count > 1)
                {
                    var u = await _loginService.UserLoginAndPassword(_selectedUser.DisplayName, _password);

                    if (u != null)
                    {
                        this.Password = null;

                        this.ErrorMessage = "LoginWelcome".GetStringFromResource(this.Language);

                        await Task.Delay(500);

                        this.ErrorMessage = null;

                        _navigationService.NavigateTo("hub", u);
                    }
                    else
                    {
                        this.Password = null;
                        this.ErrorMessage = "LoginPasswordError".GetStringFromResource(this.Language);
                    }
                }
            }
        }
        
        public User SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                Set<User>(() => SelectedUser, ref _selectedUser, value);
                _loginCommand.RaiseCanExecuteChanged();
                _userService.LoadUsers();
            }
        }
        public String Password
        {
            get
            {
                return _password;
            }
            set
            {
                Set<String>(() => Password, ref _password, value);
                _loginCommand.RaiseCanExecuteChanged();
            }
        }

        public String DisplayName
        {
            get
            {
                return _userName;
            }
            set
            {
                Set<String>(() => DisplayName, ref _userName, value);
                _loginCommand.RaiseCanExecuteChanged();
            }
        }

        public String ErrorMessage
        {
            get
            {
                return _errorMessage;
            }
            set
            {
                Set<String>(() => ErrorMessage, ref _errorMessage, value);
            }
        }

        public ObservableCollection<User> User
        {
            get
            {
                return _users;
            }
            set
            {
                Set<ObservableCollection<User>>(() => User, ref _users, value);
                _userService.LoadUsers();
            }
        }

        public string Language { get; set; }
    }
}
